/***********************************************************************************************************************
    @file    bsp_exti_interrupt.c
    @author  FAE Team
    @date    25-May-2023
    @brief   THIS FILE PROVIDES ALL THE SYSTEM FUNCTIONS.
  **********************************************************************************************************************
    @attention

    <h2><center>&copy; Copyright(c) <2023> <MindMotion></center></h2>

      Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
    following conditions are met:
    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
       the following disclaimer in the documentation and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or
       promote products derived from this software without specific prior written permission.

      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *********************************************************************************************************************/

/* Define to prevent recursive inclusion */
#define _EXTI_INTERRUPT_C_

/* Files include */
#include <stdio.h>
#include "bsp.h"

/**
  * @addtogroup MM32F0140_LibSamples
  * @{
  */

/**
  * @addtogroup EXTI
  * @{
  */

/**
  * @addtogroup EXTI_Interrupt
  * @{
  */

/* Private typedef ****************************************************************************************************/

/* Private define *****************************************************************************************************/

/* Private macro ******************************************************************************************************/

/* Private variables **************************************************************************************************/
static SIGNAL_E mic_signal_value = SIGNAL_NONE;
static SIGNAL_E chg_det_value = SIGNAL_NONE;
/* Private functions **************************************************************************************************/

/***********************************************************************************************************************
  * @brief
  * @note   none
  * @param  none
  * @retval none
  *********************************************************************************************************************/
void BSP_EXTI_Configure(void)
{
    EXTI_InitTypeDef EXTI_InitStruct;
    GPIO_InitTypeDef GPIO_InitStruct;
    NVIC_InitTypeDef NVIC_InitStruct;

    /***********************MIC************************/
    RCC_AHBPeriphClockCmd(MIC_PIN_RCC_AHBENR, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2ENR_SYSCFG, ENABLE);

    /* MIC->PD6->EXTI_Line6 */
    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin  = MIC_PIN;
    GPIO_InitStruct.GPIO_Mode = MIC_MODE;
    GPIO_Init(MIC_PORT, &GPIO_InitStruct);

    SYSCFG_EXTILineConfig(MIC_PIN_EXTI_PortSource, MIC_PIN_EXTI_PinSource);

    EXTI_StructInit(&EXTI_InitStruct);
    EXTI_InitStruct.EXTI_Line    = MIC_PIN_EXTI_Line;
    EXTI_InitStruct.EXTI_Mode    = EXTI_Mode_Interrupt;
    EXTI_InitStruct.EXTI_Trigger = MIC_PIN_EXTI_Trigger;
    EXTI_InitStruct.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStruct);

    /* EXTI Interrupt */
    NVIC_InitStruct.NVIC_IRQChannel = MIC_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPriority = 0x01;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);
    
    
    /***********************CHG_DET************************/
    RCC_AHBPeriphClockCmd(CHG_DET_PIN_RCC_AHBENR, ENABLE);

    /* CHG_DET->PB1->EXTI_Line1 */
    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin  = CHG_DET_PIN;
    GPIO_InitStruct.GPIO_Mode = CHG_DET_MODE;
    GPIO_Init(CHG_DET_PORT, &GPIO_InitStruct);

    SYSCFG_EXTILineConfig(CHG_DET_PIN_EXTI_PortSource, CHG_DET_PIN_EXTI_PinSource);

    EXTI_StructInit(&EXTI_InitStruct);
    EXTI_InitStruct.EXTI_Line    = CHG_DET_PIN_EXTI_Line;
    EXTI_InitStruct.EXTI_Mode    = EXTI_Mode_Interrupt;
    EXTI_InitStruct.EXTI_Trigger = CHG_DET_PIN_EXTI_Trigger;
    EXTI_InitStruct.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStruct);

    /* EXTI Interrupt */
    NVIC_InitStruct.NVIC_IRQChannel = CHG_DET_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPriority = 0x01;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);
}

/***********************************************************************************************************************
  * @brief
  * @note   none
  * @param  value
  * @retval none
  *********************************************************************************************************************/
void BSP_EXTI_Set_MIC_Signal(SIGNAL_E value)
{
    mic_signal_value = value;
}

/***********************************************************************************************************************
  * @brief
  * @note   none
  * @param  none
  * @retval SIGNAL_E
  *********************************************************************************************************************/
SIGNAL_E BSP_EXTI_Get_MIC_Signal(void)
{
    return mic_signal_value ;
}

/***********************************************************************************************************************
  * @brief
  * @note   none
  * @param  value
  * @retval none
  *********************************************************************************************************************/
void BSP_EXTI_Set_CHG_DET_Signal(SIGNAL_E value)
{
    chg_det_value = value;
}

/***********************************************************************************************************************
  * @brief
  * @note   none
  * @param  none
  * @retval SIGNAL_E
  *********************************************************************************************************************/
SIGNAL_E BSP_EXTI_Get_CHG_DET_Signal(void)
{
    return chg_det_value ;
}


/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/********************************************** (C) Copyright MindMotion **********************************************/

